package com.ruoyi.web.controller.bookkeeping;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.bookkeeping.domain.param.AddBookAssetParam;
import com.ruoyi.bookkeeping.domain.param.GetBookAssetParam;
import com.ruoyi.bookkeeping.domain.param.UpdateBookAssetParam;
import com.ruoyi.bookkeeping.domain.vo.*;
import com.ruoyi.bookkeeping.util.GetPageUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bookkeeping.domain.BookAsset;
import com.ruoyi.bookkeeping.service.IBookAssetService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 收支Controller
 * 
 * @author ruoyi
 * @date 2023-02-24
 */
@RestController
@RequestMapping("/bookkeeping/asset")
@Api(tags = "收支模块")
public class BookAssetController extends BaseController
{
    @Autowired
    private IBookAssetService bookAssetService;

    @GetMapping("/list")
    @ApiOperation("查询当前用户的收支列表")
    public TableDataInfoVo<List<BookAssetVo>> list(GetBookAssetParam getBookAssetParam)
    {

        Page page = GetPageUtil.getPages(getBookAssetParam.getPageNum(),getBookAssetParam.getPageSize(),bookAssetService.selectBookAssetList(getBookAssetParam,getUserId()));
        return getDataTable(page.getRecords(),page.getTotal());
    }

    @Log(title = "收支", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("根据条件导出当前用户的收支列表")
    public void export(HttpServletResponse response, GetBookAssetParam getBookAssetParam)
    {
        List<BookAsset> list = bookAssetService.selectExportBookAssetList(getBookAssetParam,getUserId());
        ExcelUtil<BookAsset> util = new ExcelUtil<BookAsset>(BookAsset.class);
        util.exportExcel(response, list, "收支数据");
    }

    @GetMapping(value = "/{assetId}")
    @ApiOperation("获取收支详细信息")
    public MsgResult<BookAssetVo> getInfo(@PathVariable("assetId") Long assetId)
    {
        return success(bookAssetService.selectBookAssetByAssetId(assetId));
    }

    @Log(title = "收支", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增收支")
    public MsgResult add(@RequestBody AddBookAssetParam addBookAssetParam)
    {
        return toAjax(bookAssetService.insertBookAsset(addBookAssetParam,getUserId()));
    }

    @Log(title = "收支", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改收支")
    public MsgResult edit(@RequestBody UpdateBookAssetParam updateBookAssetParam)
    {
        return toAjax(bookAssetService.updateBookAsset(updateBookAssetParam,getUserId()));
    }

    @Log(title = "收支", businessType = BusinessType.DELETE)
	@DeleteMapping("/{assetIds}")
    @ApiOperation("删除收支")
    public MsgResult remove(@PathVariable Long[] assetIds)
    {
        return toAjax(bookAssetService.deleteBookAssetByAssetIds(assetIds,getUserId()));
    }

    @GetMapping("/pie")
    @ApiOperation("当月支出饼图")
    public MsgResult<PieVoList> pie(String str) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        Date date = simpleDateFormat.parse(str);
        return success(bookAssetService.pie(date,getUserId()));
    }

    @GetMapping("/progressComparison")
    @ApiOperation("当月支排行榜")
    public MsgResult<List<PieVo>> progressComparison(String str) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        Date date = simpleDateFormat.parse(str);
        return success(bookAssetService.progressComparison(date,getUserId()));
    }

    @GetMapping("/histogram")
    @ApiOperation("近6个月的收入支出柱状图")
    public MsgResult<List<HistogramListVo>> histogram(){
        return success(bookAssetService.histogram(getUserId()));
    }


    @GetMapping("/monthMaxAndMin")
    @ApiOperation("近六个月最高和最低消费")
    public MsgResult<List<BookAssetListVo>> monthMaxAndMin(){
        return success(bookAssetService.monthMaxAndMin(getUserId()));
    }

}
